import {
  Body,
  Controller,
  Delete,
  forwardRef,
  Get,
  Inject,
  Put,
  Query,
} from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'

import { IdParam } from '~/common/decorators/id-param.decorator'
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator'
import { definePermission, Perm } from '~/modules/auth/decorators/permission.decorator'

import { UserSosService } from '~/modules/health/user/userSos.service'
import { SseService } from '~/modules/sse/sse.service'
import { UserSosQueryDto, UserSosUpdateDto } from './userSos.dto'

export const permissions = definePermission('health:userSos', {
  LIST: 'list',
  CREATE: 'create',
  READ: 'read',
  UPDATE: 'update',
  DELETE: 'delete',
} as const)

@ApiTags('Health - 呼救数据模块')
@ApiSecurityAuth()
@Controller('users/sos')
export class UserSosController {
  constructor(
    private userSosService: UserSosService,
    @Inject(forwardRef(() => SseService))
    private sseService: SseService,
  ) {}

  @Get('')
  @ApiOperation({ summary: '呼救数据查询' })
  @Perm(permissions.LIST)
  async listSos(@Query() dto: UserSosQueryDto): Promise<any> {
    return this.userSosService.listSos(dto)
  }

  @Put(':id')
  @ApiOperation({ summary: '更新呼救数据' })
  @Perm(permissions.UPDATE)
  async updateSos(@IdParam() id: number, @Body() dto: UserSosUpdateDto): Promise<void> {
    dto.endAt = new Date()
    await this.userSosService.updateSos(id, dto)
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除呼救数据' })
  @Perm(permissions.DELETE)
  async deleteSos(@IdParam() id: number): Promise<void> {
    await this.userSosService.deleteSos(id)
  }
}
